<?php
/**
 * Created by yuxianjun001@icloud.com.
 * User: wuchen
 * Date: 2019/4/1
 * Time: 11:03
 */

namespace App\Services\Goods;

use App\model\GoodsModel\PlatFormGoodsModel;
use App\model\GoodsPromotionModel\GoodsPromotionModel;
use App\Services\BaseService;
use Illuminate\Support\Facades\DB;

/**
 * 平台商品 服务层
 * Class PlatFormGoodsService
 * @package App\Services\Goods
 */
class PlatFormGoodsService extends BaseService
{
    protected $model;

    public function __construct()
    {
        $this->model = new PlatFormGoodsModel();
    }


    /**
     * 新增商品
     * @param $request
     * @return array
     */
    public function add($request)
    {
        $goods = PlatFormGoodsModel::where(
            ['name' => $request->name, 'deleted_at' => null])
            ->first();

        if (!empty($goods)) {
            return $this->response('', '存在同名商品', 500);
        }

        $this->model->name             = $request->name;
        $this->model->url              = $request->url;
        $this->model->sell_price       = $request->sellPrice;
        $this->model->purchase_price   = $request->purchasePrice;
        $this->model->settlement_price = $request->settlementPrice;
        $this->model->description      = $request->description;
        $this->model->status           = $request->status;
        $this->model->integral         = $request->integral;
        $this->model->remark           = $request->remark;

        $res = $this->model->save();

        if (!empty($res)) {
            return $this->response('', '新增商品成功');

        } else {
            return $this->response('', '新增商品失败', 500);

        }

    }

    /**
     * 编辑商品
     * @param $request
     * @return array
     */
    public function edit($request)
    {
        $goods = PlatFormGoodsModel::where(
            ['id' => $request->id, 'deleted_at' => null])
            ->first();

        if (empty($goods)) {
            return $this->response('', '商品不存在', 500);
        }

        $data = [
            'name'             => $request->name,
            'url'              => $request->url,
            'sell_price'       => $request->sellPrice,//售价
            'purchase_price'   => $request->purchasePrice,//进货价格
            'settlement_price' => $request->settlementPrice,//结算价格
            'description'      => $request->description,//简介
            'status'           => $request->status,//状态0-下架，1-上架
            'integral'         => $request->integral,//可获得积分
        ];

        $res = $this->model->where('id', $request->id)->update($data);
        if (!empty($res)) {
            return $this->response('', '商品修改成功');
        } else {
            return $this->response('', '商品修改失败', 500);
        }

    }

    /**
     * 删除商品
     * @param $request
     */
    public function del($request)
    {
        $user = PlatFormGoodsModel::where('id', $request->id)->first();
        if (empty($user)) {
            return $this->response('', '商品不存在', 404);
        }

        $res = $this->model->where('id', $request->id)->delete();

        if ($res) {
            return $this->response('', '商品删除成功');
        }

    }

    /**
     * 商品状态修改
     * @param $request
     * @return array
     */
    public function upAndDown($request)
    {
        $user = PlatFormGoodsModel::where('id', $request->id)->first();
        if (empty($user)) {
            return $this->response('', '商品不存在', 404);
        }

        $data = [
            'status' => $request->status ?: 0,//1-上架，0-下架
        ];
        $res  = $this->model->where('id', $request->id)->update($data);
        if (!empty($res)) {
            return $this->response('', '商品状态修改成功');
        }

    }


    /**
     * 商品列表
     * @param $request
     */
    public function list($request)
    {
        try {
            $dbResult = DB::table('plat_form_goods')->where('deleted_at', null);
            if (!empty($request->name)) {
                $dbResult = $dbResult->where('name', 'like', '%' . $request->name . '%');
            }
            if (!empty($request->status)) {
                $dbResult = $dbResult->where('status', $request->status);
            }

            $res = $this->model->buildPaginate($dbResult, $request);

            return $this->response($res, '查询成功');

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '查询失败', 500);
        }

    }

    /**
     * 商品详情，还需补充统计信息
     * @param $request
     */
    public function info($request)
    {
        $res = $this->model->where('id', $request->id)->first();
        if (empty($res)) {
            return $this->response('', '商品不存在', 404);
        }
        return $this->response($res->toArray(), '查询成功');
    }

    /**
     * 特惠活动列表
     * @param $request
     */
    public function promotionList($request)
    {
        try {
            $dbResult = DB::table('goods_promotion')->where(['status' => 1, 'deleted_at' => null]);
            if (!empty($request->goods_name)) {
                $dbResult = $dbResult->where('goods_name', 'like', '%' . $request->goodsName . '%');
            }
            if (!empty($request->goods_id)) {
                $dbResult = $dbResult->where('goods_id', '=', $request->goodsId);
            }

            if (!empty($request->business_name)) {
                $dbResult = $dbResult->where('business_name', 'like', '%' . $request->businessName . '%');
            }
            if (!empty($request->business_id)) {
                $dbResult = $dbResult->where('business_id', '=', $request->businessId);
            }

            if (!empty($request->num)) {
                $dbResult = $dbResult->where('num', $request->num);
            }

            if (!empty($request->date)) {
                $dbResult = $dbResult->where('date', $request->date);
            }

            $res = $this->model->buildPaginate($dbResult, $request);

            return $this->response($res, '查询成功');

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '查询失败', 500);
        }

    }

    /**
     * 申请特惠列表
     * @param $request
     */
    public function operationPromotionList($request)
    {
        try {
            $dbResult = DB::table('goods_promotion')->where(['status' => 0, 'deleted_at' => null]);
            if (!empty($request->goods_name)) {
                $dbResult = $dbResult->where('goods_name', 'like', '%' . $request->goods_name . '%');
            }
            if (!empty($request->goods_id)) {
                $dbResult = $dbResult->where('goods_id', '=', $request->goods_id);
            }

            if (!empty($request->business_name)) {
                $dbResult = $dbResult->where('business_name', 'like', '%' . $request->business_name . '%');
            }
            if (!empty($request->business_id)) {
                $dbResult = $dbResult->where('business_id', '=', $request->business_id);
            }

            if (!empty($request->num)) {
                $dbResult = $dbResult->where('num', $request->num);
            }

            $res = $this->model->buildPaginate($dbResult, $request);

            return $this->response($res, '查询成功');

        } catch (\Exception $e) {
            return $this->response($e->getMessage(), '查询失败', 500);
        }

    }

    /**
     * 同意特惠申请
     * @param $request
     */
    public function agreePromotion($request)
    {
        try {
            $promotion = GoodsPromotionModel::where('id', $request->id)->first();
            if (empty($promotion)) {
                return $this->response('', '该特惠商品不存在', 404);
            }

            if ($promotion->status == 1) {
                return $this->response('', '该商品已经是特惠商品', 404);
            }

            $data = [
                'status' => 1,//1-通过
                'date'   => date('Y-m-d', strtotime('+1 day')),
            ];
            $res  = GoodsPromotionModel::where('id', $request->id)->update($data);
            if (!empty($res)) {
                return $this->response('', '该特惠申请通过!');
            }

        } catch (\Exception $e) {
            return $this->response('', '该特惠异常!');
        }

    }

}